﻿/**
 * description：
 * author：Anpt
 */
define([
    "dojo/_base/declare",
    "dojo/dom-construct","dojo/on",
    "dojo/_base/lang","esri/graphic",
    "gistech/widgets/confirmDialog/ConfirmDialog","esri/layers/GraphicsLayer","esri/symbols/SimpleMarkerSymbol",
    "gistech/widgets/normalDialog/NormalDialog","esri/symbols/SimpleLineSymbol","esri/Color",
    "dojo/text!./templates/FeatureInfoDialog.html"],
    function(declare,domConstruct,on,lang,Graphic,ConfirmDialog,GraphicsLayer,SimpleMarkerSymbol,NormalDialog,SimpleLineSymbol,Color,FeatureInfoDialog){
	    return declare([NormalDialog],{
	        attr:null,//graphic类型
	        templateString:FeatureInfoDialog,
	        map:null,
	        pageObj:[],
	        pageCount:null,
	        currPageWd:null,
	        flayerurl:"",
    		moduleName:"",
    		canEdit:false,
	        constructor:function(params){
	        	this.map=params.map;
	            if(params.feaSet){
	               this.feaSet = params.feaSet;
	            }
	        },
	        createTable:function(param,domNode){
	        	dojo.empty(domNode);
	        	dojo.forEach(param.attr,lang.hitch(this,function(item,index){
	        		var tr=domConstruct.create("tr");
	        		var labtd=domConstruct.create("td",{class:"key"});
	        		var labspan=domConstruct.create("span",{innerHTML:item.alias},labtd);
	        		var value=null;
                    if(item.type=="esriFieldTypeDate"||item.type=="esriFieldTypeDouble"||item.type=="esriFieldTypeString"){value=this.formateDateOrNumber(item);}else{value=item.value}
	        		var inputtd=domConstruct.create("td",{class:"value"})
	        		var input=domConstruct.create("span",{innerHTML: value},inputtd);
	        		domConstruct.place(labtd,tr);
	        		domConstruct.place(inputtd,tr);
	        		domConstruct.place(tr,domNode);
	        	}));
	        	this.highLightGeo(param.geo);
	        	var addlayers = Global.mapshasaddlayers[Global.activeMapId];
        		for(var i=0;i<addlayers.length;i++){
        			var item = addlayers[i].layerObj;
        			if(this.feaSet[0].layerid.indexOf(item.RESOURCE_ID)!=-1){
        				this.flayerurl = item.FEATURE_URL+'/'+item.LAYER_INDEX;
        				this.moduleName = item.RESOURCE_NAME;
        				this.canEdit = item.EDITTABLE=="1"?true:false;
        				break;
        			}
        		};
	        	if(this.canEdit&&ioc.map.userInfoData.isManager&&ioc.map.userInfoData.isManager==true&&this.feaSet[0].layerid!=null){
	        		dojo.query(".siteInfoDialogRoot .dealerStoreContent").style("height", Math.ceil(param.attr.length*25.8>350?350:(param.attr.length*25.8))+"px");
	        		dojo.query(".siteInfoDialogRoot div[id=btndiv]").style("display", "block");
				}
	        },
	        highLightGeo:function(geometry){
	        	var symbol=null;
	        	var linesymbol=new SimpleLineSymbol(SimpleLineSymbol.STYLE_SOLID,new Color([255,0,0]),2);
	        	if(geometry.type=='point'){
	        		symbol=new SimpleMarkerSymbol(SimpleMarkerSymbol.STYLE_SQUARE, 16,
	        				linesymbol,new Color([255,255,255,0]));
	        	}else{
	        		symbol=linesymbol;
	        	}
	        	this.map.getLayer("highLightLayer").clear();
            	this.map.getLayer("highLightLayer").add(new Graphic(geometry,symbol));
	        },
	        featureForTree:function(fea){
	        	var featureAttr=[];
	        	this.pageObj=[];
	        	dojo.forEach(fea.fields,function(field,index){
	        		featureAttr.push({'alias':field.alias,'name':field.name,'value':fea.features.attributes[field.name],'type':field.type})
	        	})
	        	this.pageObj.push({"attr":featureAttr,"geo":fea.features.geometry});
	        	this.createTable(this.pageObj[0],this.attrTbody);
	        	
	        },
	        formateDateOrNumber:function(args){
	        	if(args.type=="esriFieldTypeDate"){
	        		if($.trim(args.value)!=""){
	        			return  DataFormate(args.value);
	        		}else{
	        			return "<空>";
	        		}
	        	}else if(args.type=="esriFieldTypeDouble"){
	        		return numberFormate(args.value);
	        	}else if(args.type=="esriFieldTypeString" ){
	        		return  args.value;
	        	}else{
	        		if(isNaN(parseFloat(args.value))){
	        			if($.trim(args.value)!=""){
		        			return  args.value;
		        		}else{
		        			return "<空>";
		        		}
	        		}else{
	        			return numberFormate(args.value);
	        		}
	        	}},
	        resetAttr:function(){
	        	this.feaSet=null;
	        	this.map.getLayer("highLightLayer").clear();
	        },
	        reCreateTable:function(params){
	        	if(params.feaSet){
		            this.feaSet = params.feaSet;
		            lang.hitch(this,this.featureForTree(this.feaSet[0]));
		        } 
	        },
	        editProjectInfo:function(){
	        	debugger;
	        	if(Global.widgets["menubar"]){
	        		Global.widgets["menubar"].openProjectManageWidget(this.moduleName,this.feaSet[0],this.flayerurl,"resavestate");
	        	}
	        },
	        postCreate:function(){
	            this.inherited(arguments);
	            this.setTitle("属性查看");
	            if(this.feaSet){
	            	lang.hitch(this,this.featureForTree(this.feaSet[0]));
	            }; 
	            on(this.edit, "click",lang.hitch(this,this.editProjectInfo));
	        },

	        //将文本框处于编辑状态
	        enableAll:function(){
	            dojo.query("td:last-child *",this.attrTable).attr("disabled",false);
	        },

	        //禁用编辑
	        disableAll:function(){
	            dojo.query("td:last-child *",this.attrTable).attr("disabled",true);
	        },
	        
	        //poccTime转化成时间
	        getPocc:function(){
	        	var pocc = parseInt(this.pocc_date.value);//字符串转成数
	        	var date = new Date(pocc);
	        	this.pocc_date.value = date;
	        },

	        startup:function(){
	            this.inherited(arguments);
	            this.placeAt(dojo.body());
	        }
	});
});